package br.com.goldentrade.util.table;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Map;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;

public class ComponentTable extends TableBase{
	
	private Border border;
	
	private boolean headerListener=true;
	
	public ComponentTable(){
		super(new TableModelBase(new ArrayList(), new String[]{}, new String[]{}), new Integer[]{}, new Integer[]{}, true);
	}
	
	public ComponentTable(String[] cols, String[] atts, Integer[] sizes, Integer[] aligns, boolean listener){
		super(new TableModelBase(new ArrayList(), cols, atts), sizes, aligns, listener);
	}
	
	public void clear() {
		
		this.getDados().clear();
		this.updateUI();
	}

	public Map getParameters() {
		return null;
	}

	public Object getValue() {
		return this.getDados();
	}

	public boolean isComponentSimples() {
		return true;
	}

	public boolean isObrigorio() {
		return false;
	}

	public void setState(boolean state) {
		
		this.setEnabled(state);
		
	}

	public void setValue(Object objeto) {
		if(objeto instanceof ArrayList){
			this.setDados(((ArrayList)objeto));
		}
	}
	
	public void setItens(String itens){
		if(itens!=null && !itens.equals("")){
			
			if(itens.contains("resizeoff")){
				this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);	
			}
			
			if(itens.contains("headeroff")){
				setHeaderListener(false);
			}
			
			if(itens.contains("rowheight=")){
				String[] strs = itens.split(";");
				for (int i = 0; i < strs.length; i++) {
					String str = strs[i];
					if(str.contains("rowheight=")){
						String[] strs2 = str.split("=");
						if(strs2.length>1){
							try {
								int height = new Integer(strs[2]);
								this.setRowHeight(height);
								break;
							} catch (Exception e) {
								continue;
							}
						}
					}
				}
			}
		}
	}

	public void valida()  {
		
	}

	public void setComponentBorder(Border border) {
		
		if(border!=null){
			if(this.border ==null){
				this.border = this.getBorder();
			}
			
			if(this.getParent() instanceof JScrollPane){
				this.border = ((JScrollPane)this.getParent()).getBorder();
				((JScrollPane)this.getParent()).setBorder(border);
			}else{
				this.setBorder(border);
			}
			
		}else{
			
			if(this.getParent() instanceof JScrollPane){
				((JScrollPane)this.getParent()).setBorder(this.border);
			}else{
				this.setBorder(this.border);
			}
		}
		
	}
	
	public boolean isHeaderListener() {
		return headerListener;
	}

	public void setHeaderListener(boolean headerListener) {
		this.headerListener = headerListener;
	}

	public void requestFocusComponent() {
		this.requestFocus();
		
	}
	
	public Component getComponentEditor() {
		return this;
	}

}
